 package action;

import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import model.Disciplina;

import padraoMemento.MementoDisciplina;

public class EditarDisciplinaAlteradaAction extends ManterDisciplinaAction {

    @Override
    public void setParametros(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        request.setAttribute("editar", "readonly");
        request.setAttribute("excluir", "");
        int codDisciplina = Integer.parseInt(request.getParameter("codDisciplina"));
        Disciplina disciplina = MementoDisciplina.obterDisciplina(codDisciplina);
             request.setAttribute("disciplina", disciplina);
        String preRequisitosSerialized = "";
        for (Disciplina preRequisito : disciplina.getDisciplinasForCodDisciplinaPreRequisito()) {
            preRequisitosSerialized = preRequisitosSerialized + preRequisito.getCodDisciplina() + ",";
        }
        request.setAttribute("preRequisitosSerialized", preRequisitosSerialized);
    }

    @Override
    public void efetuaPersistencia(Disciplina disciplina) throws ServletException {
     
        try {
             disciplina.editar();    
             MementoDisciplina.excluirDisciplina(disciplina);
           
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

}
